package com.anolesoft.amljmes.fortysix.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.fortysix.controller.MesBzzFollowController;
import com.anolesoft.amljmes.fortysix.controller.MesBzzQtfxController;
import com.anolesoft.amljmes.fortysix.dto.MesBzzFollowDto;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtdDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface MesBzzFollowServiceMapper {
    // 查询
    @SelectProvider(type=MesBzzFollowServiceMapper.createSQL.class, method = "queryMesBzzFollowSQL")
    List<MesBzzFollowDto> queryMesBzzFollowSQL(MesBzzFollowController.DemoVO demoVO);
    public static class createSQL {
        public static String queryMesBzzFollowSQL(MesBzzFollowController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid         as key,\n" +
                            "       a.objuid,\n" +
                            "       a.dingdanhao,\n" +
                            "       a.peitaodanhao,\n" +
                            "       a.ptdmx_obj,\n" +
                            "       a.hegebiaoqian,\n" +
                            "       a.paigonghao,\n" +
                            "       a.jixing,\n" +
                            "       a.tuhao,\n" +
                            "       a.jiaofuchejian,\n" +
                            "       a.jieshouchejian,\n" +
                            "       a.jiaofushunxu,\n" +
                            "       a.shuliang,\n" +
                            "       b.name           caozuoren,\n" +
                            "       a.caozuotime,\n" +
                            "       a.xianghao,\n" +
                            "       c.name           jianyanren,\n" +
                            "       a.jianyantime,\n" +
                            "       a.factory_id,\n" +
                            "       a.type,\n" +
                            "       a.pici,\n" +
                            "       a.jiaci,\n" +
                            "       d.name           shenpiren,\n" +
                            "       a.shenpitime,\n" +
                            "       e.name           tishenren,\n" +
                            "       a.tishentime,\n" +
                            "       f.name           chehiuren,\n" +
                            "       a.chehiutime,\n" +
                            "       a.fathernumber,\n" +
                            "       a.fathername,\n" +
                            "       a.proportion,\n" +
                            "       0                as pageNo,\n" +
                            "       10               as pageSize\n" +
                            "  from plan_goods_follow a\n" +
                            "  left join stf_employee b\n" +
                            "    on a.caozuoren = b.objuid\n" +
                            "  left join stf_employee c\n" +
                            "    on a.jianyanren = c.objuid\n" +
                            "  left join stf_employee d\n" +
                            "    on a.shenpiren = d.objuid\n" +
                            "  left join stf_employee e\n" +
                            "    on a.tishenren = e.objuid\n" +
                            "  left join stf_employee f\n" +
                            "    on a.chehiuren = f.objuid\n" +
                            " where 1 = 1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getPeitaodanhao())){
                        String peitaodanhao = demoVO.getPeitaodanhao();
                        sql.append(" and upper(a.peitaodanhao) = trim(upper(#{peitaodanhao, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getHegebiaoqian())){
                        String hegebiaoqian = demoVO.getHegebiaoqian();
                        sql.append(" and upper(a.hegebiaoqian) = trim(upper(#{hegebiaoqian, jdbcType=VARCHAR}))");
                    }
                    if(demoVO.getType()!= null){
                        sql.append(" and a.type = trim(#{type, jdbcType=NUMERIC})");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryId())){
                        String factoryId = demoVO.getFactoryId();
                        sql.append(" and upper(a.factory_id) = upper(#{factoryId, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    @Insert("insert into plan_goods_follow" +
            "(dingdanhao,peitaodanhao,ptdmx_obj," +
            "   hegebiaoqian,paigonghao,jixing," +
            "   tuhao,jiaofuchejian,jieshouchejian," +
            "   jiaofushunxu,shuliang,caozuoren," +
            "   caozuotime,xianghao,jianyanren," +
            "   jianyantime,factory_id,type," +
            "   pici,jiaci,fathernumber," +
            "   fathername,proportion,shenpiren," +
            "   shenpitime,tishenren,tishentime," +
            "   chehiuren,chehiutime)" +
            "values" +
            "  (#{dingdanhao, jdbcType=VARCHAR},#{peitaodanhao, jdbcType=VARCHAR},#{ptdmxObj, jdbcType=VARCHAR}," +
            "   #{hegebiaoqian, jdbcType=VARCHAR},#{paigonghao, jdbcType=VARCHAR},#{jixing, jdbcType=VARCHAR}," +
            "   #{tuhao, jdbcType=VARCHAR},#{jiaofuchejian, jdbcType=VARCHAR},#{jieshouchejian, jdbcType=VARCHAR}," +
            "   #{jiaofushunxu, jdbcType=VARCHAR}, #{shuliang, jdbcType=NUMERIC},#{caozuoren, jdbcType=VARCHAR}," +
            "   sysdate,#{xianghao, jdbcType=VARCHAR},#{jianyanren, jdbcType=VARCHAR}," +
            "   to_date(#{jianyantime, jdbcType=DATE},'yyyy-mm-dd'),#{factoryId, jdbcType=VARCHAR},#{type, jdbcType=NUMERIC}," +
            "#{pici, jdbcType=VARCHAR},#{jiaci, jdbcType=VARCHAR},#{fathernumber, jdbcType=VARCHAR}," +
            "#{fathername, jdbcType=VARCHAR},#{proportion, jdbcType=VARCHAR},#{shenpiren, jdbcType=VARCHAR}," +
            "to_date(#{shenpitime, jdbcType=DATE},'yyyy-mm-dd'),#{tishenren, jdbcType=VARCHAR},to_date(#{tishentime, jdbcType=DATE},'yyyy-mm-dd')," +
            "#{chehiuren, jdbcType=VARCHAR},to_date(#{chehiutime, jdbcType=DATE},'yyyy-mm-dd'))")
    int addMesBzzFollow(MesBzzFollowDto inFoDtoParam);
}
